package com.fmsz.xmly.activitys.broadcast;

import android.support.design.widget.TabLayout;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.fmsz.xmly.R;
import com.fmsz.xmly.adapters.broadcast.ProvincePagerAdapter;
import com.fmsz.xmly.application.MyApplication;
import com.fmsz.xmly.entitys.broadcast.ProvinceListEntity;
import com.fmsz.xmly.utils.SlideBackLayout;
import com.fmsz.xmly.widget.CustomViewPager;

import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ProvinceListActivity extends AppCompatActivity implements Callback<ProvinceListEntity>, View.OnClickListener{

    private Toolbar toolbar;
    private ActionBar actionBar;
    private ViewPager pager;
    private TabLayout tabLayout;
    private List<ProvinceListEntity.ResultEntity> list;
    private ProvincePagerAdapter pagerAdapter;
    private ImageView more_btn;
    private View popWindow;
    private DrawerLayout drawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_province_list);
        loadTitleData();
        initView();
    }

    //初始化tabLayout的title数据
    private void loadTitleData() {
        list = new ArrayList<>();
        MyApplication application = (MyApplication) getApplication();
        application.getBroadService().getProvinceList().enqueue(this);
    }

    private void initView() {
//        drawer = ((DrawerLayout) findViewById(R.id.drawer_province));
//        drawer.openDrawer(GravityCompat.END);
//        toolbar = ((Toolbar) findViewById(R.id.toolbar_province));
//        setSupportActionBar(toolbar);
//        actionBar = getSupportActionBar();
//        actionBar.setDisplayHomeAsUpEnabled(true);
//        actionBar.setDisplayShowTitleEnabled(false);
        pager = ((CustomViewPager) findViewById(R.id.pager_province));
        tabLayout = ((TabLayout) findViewById(R.id.tabLayout_pro));
        more_btn = ((ImageView) findViewById(R.id.more_btn_province));
        more_btn.setOnClickListener(this);
        pagerAdapter = new ProvincePagerAdapter(getSupportFragmentManager(), list);
        pager.setAdapter(pagerAdapter);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onResponse(Call<ProvinceListEntity> call, Response<ProvinceListEntity> response) {
        if (pagerAdapter!=null && response.body() != null) {
            list.addAll(response.body().getResult());
            pagerAdapter.notifyDataSetChanged();
            tabLayout.setupWithViewPager(pager);
        }
    }

    @Override
    public void onFailure(Call<ProvinceListEntity> call, Throwable t) {

    }

    @Override
    public void onClick(View v) {
//        PopupWindow window = new PopupWindow(popWindow, pager.getWidth(), 400);
//        window.setFocusable(false);
//        window.setOutsideTouchable(true);
//        window.showAsDropDown(tabLayout);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        AlertDialog dialog = builder.create();
        dialog.setView(LayoutInflater.from(this).inflate(R.layout.pop_window,null));
//        dialogLayoutParams = dialog.getAttributes();
        dialog.show();

    }
}
